<?php
session_start();
?>

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <title>ESHOP | Корзина</title>
    <link rel="icon" type="image/png" sizes="32x32" href="/images/other/r-favicon-32x32.png">
    <link rel="stylesheet" href="/css/cart_style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <script src="/scripts/cart_scripts.js"></script>
</head>
<body>
    <div id="cartContainer">
        <h1>Корзина</h1>
        <div id="cartItems"></div>
        <div class="totalContainer">
            <span id="totalAmount">Общая сумма: 0 руб.</span>
            <button onclick="checkout()">Оформить заказ</button>
        </div>
    </div>

    <div id="checkoutModal" class="modal" style="display:none;">
        <h2>Оформление заказа</h2>
        <div>
            <input type="radio" id="selectAddress" name="addressType" value="select" checked onchange="toggleAddressInput(false)">
            <label for="selectAddress">Выбрать адрес</label>
            <input type="radio" id="newAddress" name="addressType" value="new" onchange="toggleAddressInput(true)">
            <label for="newAddress">Новый адрес</label>
        </div>
        <div id="addressSelection" style="margin-bottom: 20px;">
            <select id="addressSelect"></select>
        </div>
        <div id="addressInput" style="display: none;">
            <label for="addressTextInput">Введите новый адрес:</label>
            <input type="text" id="addressTextInput">
        </div>
        <div>
            <h3>Выберите способ оплаты:</h3>
            <input type="radio" id="paymentOnDelivery" name="paymentMethod" value="Оплата при получении" checked>
            <label for="paymentOnDelivery">Перевод по карте</label>
            <input type="radio" id="cardToCourier" name="paymentMethod" value="Картой курьеру">
            <label for="cardToCourier">Картой курьеру</label>
        </div>
        <p id="deliveryTime"></p>
        <button id="confirmOrderButton">Оформить заказ</button>
        <button onclick="closeCheckoutModal()">Закрыть</button>
    </div>
    
    <script>
        var isLoggedIn = <?php echo json_encode(isset($_SESSION['username'])); ?>;
        var globalUserId = <?php if (isset($_SESSION['user_id'])): ?><?php echo htmlspecialchars($_SESSION['user_id']); ?><?php endif; ?>;
        document.addEventListener('DOMContentLoaded', function () {
            displayCartItems();
        });
    </script>
</body>
</html>